import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Contact } from './contact.interface';
import { Model } from 'mongoose';
import { CreateContactDTO, EditContactDTO } from './contact.dto';

@Injectable()
export class ContactService {
    constructor(@InjectModel('contact') private readonly contactModel: Model<Contact>)  {}

    
    async findAll(): Promise<Contact[]> {
        return await this.contactModel.find();
    }

    async findWhere(filter?: object): Promise<Contact[]> {
        return await this.contactModel.find(filter);
    }

    async find(id: string): Promise<Contact> {
        return await this.contactModel.findById(id);
    }
    
    async addone(body: CreateContactDTO): Promise<Contact> {
        return await this.contactModel.create(body);
    }

    async editone(id: string, body: EditContactDTO): Promise<Contact> {
        return await this.contactModel.findByIdAndUpdate(id, body);
    }
    
    async delete(id: string): Promise<Contact> {
        return await this.contactModel.findByIdAndDelete(id);
    }
}
